package com.family.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.family.gmall.common.cache.annotation.GmallCache;
import com.family.gmall.common.constant.RedisConst;
import com.family.gmall.item.service.ItemService;
import com.family.gmall.list.client.ListFeignClient;
import com.family.gmall.model.product.entity.BaseCategoryView;
import com.family.gmall.model.product.entity.SkuInfo;
import com.family.gmall.model.product.entity.SpuSaleAttr;
import com.family.gmall.product.client.ProductFeignClient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Auther: family
 * @Date: 2023/7/14 08:47
 * @Description:
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Resource
    private ListFeignClient listFeignClient;

    @Override
    @GmallCache(
            key = RedisConst.SKUINFO + "#{#params[0]}",
            lockKey = RedisConst.LOCK_SKUINFO + "#{#params[0]}",
            ttl = 180
    )
    public Map<String, Object> getItemBySkuId(Long skuId) {

        Map<String, Object> result = new HashMap<>();

        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //  获取到的数据是skuInfo + skuImageList
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            //  获取销售属性+销售属性值
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            //  获取分类数据
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            //  查询销售属性值Id 与skuId 组合的map
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            //  将这个map 转换为页面需要的Json 对象
            String valueJson = JSON.toJSONString(skuValueIdsMap);
            result.put("valuesSkuJson", valueJson);
        }, threadPoolExecutor);

        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            result.put("price", skuPrice);
        }, threadPoolExecutor);

        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        CompletableFuture.allOf(
                skuInfoFuture,
                spuSaleAttrFuture,
                categoryFuture,
                valuesSkuJsonFuture,
                priceFuture,
                incrHotScoreCompletableFuture).join();
        return result;
    }
}
